package com.bs.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/23.
 */
public class EiInfo extends JSONObject{
    public JSONObject SysInfo;
    public JSONArray Tables;
    public JSONObject table1;

    public JSONArray table1Columns;
    public JSONArray table1Rows;
    //public JSONObject table2;

    private int TableIndex = 0;
    private ArrayList<String> arrayListTableName = new ArrayList<String>();

    public EiInfo() throws JSONException,Exception{
        super();
        //jsonObj = new JSONObject();
        SysInfo = new JSONObject();
        Tables = new JSONArray();
        table1 = new JSONObject();
        table1Columns = new JSONArray();
        table1Rows =  new JSONArray();

        table1.put("Name", "Table0");
        table1.put("Columns",table1Columns);
        table1.put("Rows",table1Rows);
        Tables.put(0,table1);
        arrayListTableName.add("Table0");

        this.put("Tables", Tables);
        this.put("SysInfo", SysInfo);
        TableIndex++;
    }

    public EiInfo(String JsonString)  throws JSONException{
        super(JsonString);
        SysInfo = (JSONObject)this.get("SysInfo");
        Tables = (JSONArray)this.get("Tables");
        table1 = (JSONObject)Tables.get(0);

        arrayListTableName.clear();
        for(int i = 0; i < Tables.length(); i++){
            JSONObject t = (JSONObject)Tables.get(i);
            String tName = t.getString("Name");
            arrayListTableName.add(tName);
            TableIndex++;
        }

        table1Columns = (JSONArray)table1.get("Columns");
        if(table1Columns.length() > 0) {
            JSONObject table1Column1 = (JSONObject) table1Columns.get(0);
            String table1Column1Name = (String) table1Column1.get("Name");
        }

        table1Rows = (JSONArray)table1.get("Rows");
    }

    public EiInfo(String JsonString, boolean flag) throws JSONException{
        super(JsonString);
        SysInfo = (JSONObject)this.get("SysInfo");
        Tables = (JSONArray)this.get("Tables");
        table1 = (JSONObject)Tables.get(0);

        arrayListTableName.clear();
        for(int i = 0; i < Tables.length(); i++){
            JSONObject t = (JSONObject)Tables.get(i);
            String tName = t.getString("Name");
            arrayListTableName.add(tName);
            TableIndex++;
        }

        if(flag) {
            table1Columns = (JSONArray) table1.get("Columns");
            JSONObject table1Column1 = (JSONObject) table1Columns.get(0);
            String table1Column1Name = (String) table1Column1.get("Name");

            table1Rows = (JSONArray) table1.get("Rows");
        }
    }

    public List<String> GetList(int index) throws JSONException{
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < table1Rows.length(); i++) {
            JSONArray table1Row1 = (JSONArray)table1Rows.get(i);
            String table1Row1Col1 = (String)table1Row1.get(index);
            list.add(table1Row1Col1);
        }
        return list;
    }

    public List<Map<String, Object>> GetList() throws JSONException{
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        for (int i = 0; i < table1Rows.length(); i++) {
            JSONArray table1Row1 = (JSONArray) table1Rows.get(i);
            map = new HashMap<String, Object>();
            for (int j = 0; j < table1Columns.length(); j++) {
                JSONObject table1Column1 = (JSONObject)table1Columns.get(j);
                String table1Column1Name = (String)table1Column1.get("Name");
                String table1Row1Col1 = (String) table1Row1.get(j);
                map.put(table1Column1Name, table1Row1Col1);
            }
            list.add(map);
        }
        return list;
    }

    public String table1Cell(int row, int col) throws JSONException{
        JSONArray table1Row1 = (JSONArray)table1Rows.get(row);
        return (String)table1Row1.get(col);
    }

    public String Cell(int table, int row, int col) throws JSONException{
        JSONObject tableX = (JSONObject)Tables.get(table);
        JSONArray tableXRows = (JSONArray)tableX.get("Rows");
        JSONArray tableXRow1 = (JSONArray)tableXRows.get(row);
        return (String)tableXRow1.get(col);
    }

    //新增表
    public void AddTable(String tableName) throws JSONException, Exception{
        if(arrayListTableName.contains(tableName))
            throw new Exception("表名[" + tableName + "]已存在");

        JSONObject table = new JSONObject();
        JSONArray column = new JSONArray();
        JSONArray row = new JSONArray();

        table.put("Name", tableName);
        table.put("Columns",column);
        table.put("Rows",row);

        TableIndex = Tables.length();
        Tables.put(TableIndex, table);
        arrayListTableName.add(tableName);
        TableIndex++;
    }

    //新增表
    public void AddTable(DataTable table) throws JSONException, Exception{
        String tableName = table.getTableName();
        if(arrayListTableName.contains(tableName))
            throw new Exception("表名[" + tableName + "]已存在");

        JSONObject jsTable = new JSONObject();
        JSONArray jsCol = new JSONArray();
        JSONArray jsRow = new JSONArray();

        //col
        for(int i = 0; i < table.getColumns().size(); i++){
            jsCol.put(i,new JSONObject("{'Name':'" + table.getColumns().get(i).getColumnName() + "'}"));
        }

        //row
        for(int i = 0; i < table.getRows().size(); i++){
            JSONArray arrayRow = new JSONArray();
            for(int  j = 0; j < table.getColumns().size(); j++){
                arrayRow.put(table.getValue(i,j));
            }
            jsRow.put(i, arrayRow);
        }

        //table
        jsTable.put("Name", tableName);
        jsTable.put("Columns",jsCol);
        jsTable.put("Rows",jsRow);

        TableIndex = Tables.length();
        Tables.put(TableIndex, jsTable);
        arrayListTableName.add(tableName);
        TableIndex++;
    }

    //删除表
    public void RemoveTable(int index) throws JSONException{
        JSONArray cpTable = new JSONArray();
        for(int i = 0; i < Tables.length(); i++){
            if(i != index){
                DataTable table = (DataTable)Tables.get(i);
                cpTable.put(table);
            }
        }
        Tables = cpTable;
        arrayListTableName.remove(index);
    }

    //删除表
    public void RemoveTable(String tableName) throws JSONException{
        int tableIndex = arrayListTableName.indexOf(tableName);
        RemoveTable(tableIndex);
    }

    public void UpdateTableName(int tableIndex, String newTableName) throws JSONException{
        JSONObject table = (JSONObject) Tables.get(tableIndex);
        table.put("TableName", newTableName);
        arrayListTableName.set(tableIndex, newTableName);
    }

    public void UpdateTableName(String oldTableName, String newTableName) throws JSONException{
        int tableIndex = arrayListTableName.indexOf(oldTableName);
        UpdateTableName(tableIndex, newTableName);
    }

    //获得表
    public DataTable GetTable(int index) throws JSONException, Exception{
        if(index < 0)
            throw new JSONException("表不存在");

        DataTable table = new DataTable();
        //TableName
        JSONObject jsTable = (JSONObject)Tables.get(index);
        if(jsTable.has("TableName")) {
            String tableName = jsTable.getString("TableName");
            table.setTableName(tableName);
        }
        //Column
        JSONArray jsCol = (JSONArray)jsTable.get("Columns");
        int colCount = jsCol.length();
        for(int i = 0; i < colCount; i++){
            JSONObject jscol = (JSONObject)jsCol.get(i);
            String colName = jscol.getString("Name");
            String dataType = jscol.getString("DataType");
            table.addColumn(colName, dataType);
        }
        //Rows
        JSONArray jsRow = (JSONArray)jsTable.get("Rows");
        for(int i = 0; i < jsRow.length(); i++){
            DataRow row = table.newRow();
            for(int j = 0; j < colCount; j++){
                Object value = ((JSONArray)jsRow.get(i)).get(j);
                row.setValue(j,value);
            }
            table.addRow(row);
        }
        return table;
    }

    //获得表
    public DataTable GetTable(String tableName) throws JSONException, Exception{
        int index = arrayListTableName.indexOf(tableName);
        return GetTable(index);
    }

    //新增列
    public void AddColumn(int tableIndex, String colName, String dataType) throws JSONException{
        JSONObject table = (JSONObject) Tables.get(tableIndex);
        JSONArray cols = (JSONArray)table.get("Columns");
        JSONArray rows = (JSONArray)table.get("Rows");

        int colsCount = cols.length();
        String js = "{'Name':'" + colName + "','DataType':'" + dataType + "'}";
        cols.put(colsCount, new JSONObject(js));

        for(int i = 0; i < rows.length(); i++){
            JSONArray row = (JSONArray)rows.get(i);
            row.put("");
        }
    }

    //新增列
    public void AddColumn(int tableIndex, String colName) throws JSONException{
        AddColumn(tableIndex, colName, "S");
    }

    //新增列
    public void AddColumn(String tableName, String colName, String dataType) throws JSONException{
        int tableIndex = arrayListTableName.indexOf(tableName);
        AddColumn(tableIndex, colName, dataType);
    }

    //新增列
    public void AddColumn(String tableName, String colName) throws JSONException{
        int tableIndex = arrayListTableName.indexOf(tableName);
        AddColumn(tableIndex, colName, "S");
    }

    //新增行
    public void AddRow(int tableIndex, Object... object) throws JSONException, Exception{
        JSONObject table = (JSONObject) Tables.get(tableIndex);
        JSONArray cols = (JSONArray)table.get("Columns");
        JSONArray rows = (JSONArray)table.get("Rows");

        if(object.length >  cols.length())
            throw new Exception("传入值数量大于列");

        JSONArray arrayrow = new JSONArray();
        for(Object o : object){
            arrayrow.put(o);
        }
        rows.put(rows.length(), arrayrow);
    }

    //新增行
    public void AddRow(String tableName, Object... object) throws JSONException, Exception{
        int tableIndex = arrayListTableName.indexOf(tableName);
        AddRow(tableIndex, object);
    }
}
